﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFTemplate.ViewModel;

namespace WPFTemplate.TemplateDic
{
    public  class ViewModelTypeDic
    {
        private readonly Dictionary<string, Type> _typeDic = new Dictionary<string, Type>();

        public ViewModelTypeDic()
        {
            AddMapping("APerson", typeof(APersonViewModel));
            AddMapping("BPerson", typeof(BPersonViewModel));
        }

        public  void AddMapping(string name, Type type)
        {
            _typeDic[name] = type;
        }

        public ViewModelBase CreateInstance(string typeName)
        {
            Type type;
            if (!_typeDic.TryGetValue(typeName, out type))
            {
                throw new ArgumentException($"Type '{typeName}' is not registered.");
            }
            return Activator.CreateInstance(type) as ViewModelBase;
        }
    }
}
